package com.lemner.ke.activity;

import android.graphics.drawable.GradientDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lemner.ke.R;
import com.lemner.ke.base.BaseActivity;
import com.lemner.ke.base.BaseListener;
import com.lemner.ke.base.SuccessListener;
import com.lemner.ke.base.TopBar;
import com.lemner.ke.bean.SuccessBean;
import com.lemner.ke.model.loginreg.GetIdentifyingCodeModel;
import com.lemner.ke.model.loginreg.VerifyPhoneModel;
import com.lemner.ke.model.persional.ResetPwdModel;

import java.util.Timer;
import java.util.TimerTask;

public class ForgetPasswordActivity extends BaseActivity {
    private ImageView iv_eye;
    private Boolean eyeOpen = false;
    private EditText et_phone,et_ver_code,et_new_pwd;
    private TextView tv_get_ver;
    private Button btn__commit;
    private int time;
    @Override
    public int getLayoutFile() {
        return R.layout.activity_forget_password;
    }

    @Override
    public void initView() {
        iv_eye=findViewById(R.id.iv_forget_eye);
        et_phone=findViewById(R.id.et_forget_tp_num);
        et_ver_code=findViewById(R.id.et_forget_ver_code);
        et_new_pwd=findViewById(R.id.et_forget_new_tp_num);
        tv_get_ver=findViewById(R.id.tv_forget_get_ver);
        btn__commit=findViewById(R.id.btn_forget_password_commit);
    }

    @Override
    public void initEvent() {
        iv_eye.setOnClickListener(this);
        tv_get_ver.setOnClickListener(this);
        btn__commit.setOnClickListener(this);
    }

    @Override
    public void initData() {

    }

    @Override
    protected void initSetting() {

    }

    @Override
    public void onClick(View v) {
        final String mobile = et_phone.getText().toString();
        final String verification_code = et_ver_code.getText().toString();
        final String password = et_new_pwd.getText().toString();
        switch (v.getId()) {
            case R.id.iv_forget_eye:
                if (eyeOpen == false) {
                    iv_eye.setImageDrawable(getResources().getDrawable(R.drawable.change_phone_eye_open));
                    et_new_pwd.setInputType(1);
                    eyeOpen = true;
                } else if (eyeOpen == true) {
                    iv_eye.setImageDrawable(getResources().getDrawable(R.drawable.change_phone_eye_close));
                    et_new_pwd.setInputType(129);
                    eyeOpen = false;
                }
                break;
            case R.id.tv_forget_get_ver:
                new GetIdentifyingCodeModel().getIdentifyingCode(et_phone.getText().toString(), "identity", new SuccessListener());
                time = 60;
                final Timer timer = new Timer();
                timer.schedule(new TimerTask() {
                    @Override
                    public void run() {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    if (time <= 0) {
                                        tv_get_ver.setText("获取验证码");
                                        tv_get_ver.setClickable(true);
                                        ((GradientDrawable) tv_get_ver.getBackground()).setStroke(1, 0xff000000);
                                        tv_get_ver.setTextColor(0xff000000);
                                        timer.cancel();
                                    } else {
                                        tv_get_ver.setText("重新发送(" + time-- + "s)");
                                        tv_get_ver.setClickable(false);
                                        ((GradientDrawable) tv_get_ver.getBackground()).setStroke(1, 0xff818181);
                                        tv_get_ver.setTextColor(0xff818181);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                }
                            }
                        });
                    }
                }, 0, 1000);
                break;
            case R.id.btn_forget_password_commit:
                if (mobile.isEmpty()) {
                    showToast("请输入手机号！");
                } else if (verification_code.isEmpty()) {
                    showToast("请输入验证码！");
                } else if (password.isEmpty()) {
                    showToast("请输入密码！");
                } else if (password.length() < 6) {
                    showToast("密码长度不能少于6位！");
                } else {
                    new VerifyPhoneModel().verifyPhone(mobile, verification_code, new BaseListener<SuccessBean>() {
                        @Override
                        public void onResponse(SuccessBean successBean) {
                            new ResetPwdModel().resetPwd(mobile, password, new BaseListener<SuccessBean>() {
                                @Override
                                public void onResponse(SuccessBean successBean) {
                                    if (successBean.getCode().equals("1000")) {
                                        showToast(successBean.getMsg());
                                        startActivity(LoginActivity.class);
                                        finish();
                                    } else {
                                        showToast(successBean.getMsg());
                                    }
                                }

                                @Override
                                public void onFail(String msg) {
                                    showToast(msg);
                                }
                            });
                        }

                        @Override
                        public void onFail(String msg) {
                            showToast(msg);
                        }
                    });
                    break;
                }
        }
    }
}